<template>
	
	<view class="content">
	
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" maxlength="11" />
		</view>
		<view class="row b-b">
			<text class="tit">地址</text>
			<text @click="showMulLinkageThreePicker" class="input">
				{{addressData.addrs}}
			</text>
			
		</view>
		<view class="row b-b"> 
			<text class="tit">门牌号</text>
			<input class="input" type="text" v-model="addressData.addrmore" placeholder="楼号、门牌" placeholder-class="placeholder" />
		</view>
		
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.isdefault" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"
		 @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import mpvueCityPicker from '../../components/mpvue-citypicker/mpvueCityPicker.vue';
	import uniNavBar from "../../components/uni-nav-bar/uni-nav-bar.vue"
	
	export default {
		components: {
			mpvueCityPicker,
			uniNavBar
		},
		data() {
			return {
				cityPickerValueDefault: [0, 0, 1],
				themeColor: '#007AFF',
				addressData: {
					
					name: '',
					phone: '',
					addrs: '选择省市区',
					cityCode: '',
					addrmore: '',
					isdefault:false,
					default: 1
				}
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
				
				this.addressData = JSON.parse(option.data);
				this.addressData.isdefault=this.addressData.default==2;
				this.addressData.cityCode=this.addressData.addr.substring(this.addressData.addr.lastIndexOf(",")+1)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			switchChange(e){
				this.addressData.isdefault = e.target.value;
		
			},
			// 三级联动选择
			showMulLinkageThreePicker() {
			
				this.$refs. mpvueCityPicker.show()
			},
			onConfirm(e) {
				console.log(e);
					this.addressData.cityCode =e.cityCode;
					this.addressData.addrs = e.label;
				this.pickerText = JSON.stringify(e)
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						console.log(data);
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
					}
				})
			},
			
			//提交
			confirm(){
				let data = this.addressData;
				var isdefault = this.addressData.isdefault?2:1;
				
				if(!data.name){
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|6|9|7|8][0-9]{9}$)/.test(data.phone)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!data.cityCode){
					this.$api.msg('请选择省市区');
					return;
				}
				if(!data.addrmore){
					this.$api.msg('请填写门牌号信息');
					return;
				}
				
				uni.showLoading();
				if(this.manageType=="edit"){
				uni.request({
					url: 'http://tuiguang.yiyunoto.com/api/member/editmemaddr',
					method: 'POST',
					header:{authtoken:this.$store.state.token},
					
					data: {addrmore:data.addrmore,name:data.name,phone:data.phone,default:isdefault,addr:data.cityCode,id:this.addressData.id},
					success: res => {
						uni.hideLoading();
						this.$api.msg(res.data.msg);
						if(res.data.code==1){
							console.log(res);
							this.$api.prePage().refreshList();
							uni.navigateBack();
						}
					},
					fail: () => {},
					complete: () => {}
				});	
				}else
				uni.request({
					url: 'http://tuiguang.yiyunoto.com/api/member/addmemaddr',
					method: 'POST',
					header:{authtoken:this.$store.state.token},
					
					data: {addrmore:data.addrmore,name:data.name,phone:data.phone,default:isdefault,addr:data.cityCode},
					success: res => {
						uni.hideLoading();
						this.$api.msg(res.data.msg);
						if(res.data.code==1){
							console.log(res);
							this.$api.prePage().refreshList();
							uni.navigateBack();
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
